<?php

namespace app\api\controller;


class Testweixin
{


    public $appid = 'wx76e3dc0831fa5300';
    public $appsecret = '89a12fcb23e642e383b5fc13cceac0b7';
    public $token = "xgl3306";
    public $EncodingAESKey = 'xPeBX0vQ8w29ed9Gc6JTo3y65l5Atzsm1umvfUcpteK';

    /**
     * 接入微信公众号时 验证函数
     * @return string
     */
    public function wxcheck_sign()
    {
        $webChat = $this->getWebchatHandle();
        $webChat->valid();
        return '';
    }

    /**
     * 获取类句柄
     * @return \webChat
     */
    private function getWebchatHandle()
    {
        include_once(dirname(realpath(APP_PATH)) . "/vendor/myCal/webChat.class.php");
        $appid = $this->appid;
        $appsecret = $this->appsecret;
        $token = $this->token;
        $webChat = new \webChat($appid, $appsecret, $token);
        return $webChat;
    }

    /**
     * 用户授权成功后 获取更具code 来换取openid信息  和 根据openid 获取用户的基本信息
     */
    public function wxcheck()
    {
        $code = trim($_REQUEST['code']);//code码，微信用户授权同意后的code，用该code来换取openid
        $state = trim($_REQUEST['state']);//用户自定义的数，可以用这个数来区分业务
        $webChat = $this->getWebchatHandle();
        if ($code) {//用户授权成功后 获取openid和用户信息
            $res = $webChat->getOpenid($code);//使用code 换取用户的openid信息
            print_r($res);
            if ($res['err'] == 0) {
                $openid = $res['data']['openid'];
                $access_token = $res['data']['access_token'];
                $wxuserinfo = $webChat->getUserinfo($access_token, $openid);//根据openid来获取用户的基本信息
                print_r($wxuserinfo);
            }
        } else//用户输入 或者 点击菜单等微信推送的消息
        {
            $webChat->responseMsg();
        }

    }


    public function Index()
    {
        $state = $_REQUEST['state'];///1申请  2查查 3、代理平台
        $appid = $this->appid;
        $appsecret = $this->appsecret;
        $token = $this->token;
        $redirect_uri = 'http://api.witsleep.com/api/testweixin/wxcheck';//授权成功后的跳转地址
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $appid . "&redirect_uri=" . urlencode($redirect_uri) . "&response_type=code&scope=snsapi_userinfo&state=" . $state . "#wechat_redirect";
        echo '<!DOCTYPE html>
        <html>
        <head>
            <!-- 以下方式只是刷新不跳转到其他页面 -->
            <meta http-equiv="refresh" content="10">
            <!-- 以下方式定时转到其他页面 -->
            <meta http-equiv="refresh" content="0;url=' . $url . '">
        </head>
        <body>
        </body>
        </html>';
    }

    /**
     * 创建自定义菜单
     */
    public function createMenu()
    {
        $webChat = $this->getWebchatHandle();
        $res = $webChat->createMenu();//使用code 换取用户的openid信息
        print_r($res);
    }

    /**
     * 添加微信客服
     */
    public function addCustomerUser(){
        $hander = $this->getWebchatHandle();
        $customer = array(
            'kf_account' => 'zhangshan@gh_4d666ee014c5',//帐号前缀@公众号微信号，帐号前缀最多10个字符
            'nickname' => '张三'
        );
        //$data = json_encode($customer, JSON_UNESCAPED_UNICODE);//json编码后 中文不进行unicode编码
        $result = $hander->addCustomerUser($customer);
        print_r($result);
    }
    /**
     * 公众号发送客服消息
     */
    public function sendCustomerMessage()
    {
        $webChat = $this->getWebchatHandle();

        $openid = "ovIu5uKWLDtT-9iPvcLLHxL-RFcY";
        //1、发送文本消息
        $message = array(
            'msgtype' => 'text',
            'text' => array(
                'content' => '你好大神'
            )
        );
        //2、发送图片消息
        $message = array(
            'msgtype' => 'image',
            'image' => array(
                'media_id' => 'Jx1zp-bkvYvr2WCvGGvR-gWEznVfx-IJo6dkHqohVAA'
            )
        );
        //3、发送图文消息(点击跳转到外链)
        $message = array(
            'msgtype' => 'news',
            'news' => array(
                'articles' => [
                    array(
                        "title" => "Happy Day",
                        "description" => "Is Really A Happy Day",
                        "url" => "http://www.baidu.com",
                        "picurl" => "https://api.witsleep.com/static/admin/images/logoB.png"
                    )
                ]
            )
        );
        //4、发送菜单消息
        $message = array(
            'msgtype' => 'msgmenu',
            'msgmenu' => array(
                'head_content'=>'您对本次服务是否满意呢?',
                'list' => [
                    array(
                        'id'=>1,
                        'content'=>'满意'
                    ),
                    array(
                        'id'=>2,
                        'content'=>'不满意'
                    ),
                ],
                'tail_content'=>'欢迎再次光临'
            )
        );

        $res = $webChat->sendCustomerMessage($openid, $message);
        print_r($res);
    }

    /**
     * 新增临时性素材
     */
    public function uploadTmpMedia()
    {
        $hander = $this->getWebchatHandle();
        $access_token = $hander->getAccessToken();
        if ($access_token['err'] != 0) {
            echo $access_token['msg'];
            exit;
        }
        $access_token = $access_token['data'];

        $file_path = dirname(realpath(APP_PATH)) . "/public" . "/static/sleepSum/sleepHtml/images/logo.png";
        $res = $hander->uploadTmpMedia($access_token, $file_path, 'image');
        print_r($res);
    }

    /**
     * 新增永久性素材
     */
    public function uploadMedia()
    {
        $hander = $this->getWebchatHandle();
        $access_token = $hander->getAccessToken();
        if ($access_token['err'] != 0) {
            echo $access_token['msg'];
            exit;
        }
        $access_token = $access_token['data'];

        $file_path = dirname(realpath(APP_PATH)) . "/public" . "/static/sleepSum/sleepHtml/images/logo.png";
//        $result = $hander->uploadMedia($access_token, 2, $file_path);//只返回url
        $result = $hander->uploadMedia($access_token, 3, $file_path);//返回media_id 和url
        print_r($result);
    }

    /**
     * 新增永久性 图文素材
     */
    public function uploadMediaImageAndText()
    {
        $hander = $this->getWebchatHandle();
        $access_token = $hander->getAccessToken();
        if ($access_token['err'] != 0) {
            echo $access_token['msg'];
            exit;
        }
        $access_token = $access_token['data'];
        $data = array(
            'articles' => [
                array(
                    "title" => "成都的朋友们欢迎您！",
                    "thumb_media_id" => 'Jx1zp-bkvYvr2WCvGGvR-gWEznVfx-IJo6dkHqohVAA',
                    "author" => 'xgl',
                    "digest" => "摘要",
                    "show_cover_pic" => 1,
                    "content" => "欢迎到来",
                    "content_source_url" > 'http://www.qnchisu.com',
                    "need_open_comment" => 1,
                    "only_fans_can_comment" => 1
                ),
            ]
        );
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);//json编码后 中文不进行unicode编码
        $result = $hander->uploadMedia($access_token, 1, $data);
        print_r($result);
    }


    public function toShortUrl()
    {
        $hander = $this->getWebchatHandle();
        $longUrl ='http://api.dongshuimian.com/api/user/login?uname=13438046218&pwd=123456';
        $result = $hander->toShortUrl($longUrl);//返回media_id 和url
        print_r($result);
    }



}

